package com.cysion.otbox.utils

import android.os.Environment
import com.cysion.otbox.Box
import com.cysion.otbox.ext.tryCatch
import com.cysion.otbox.ext.tryCatchWithReSult
import java.io.File

/*
* 外部存储中业务分类文件夹命名，比如xinhua
* */
private var homeDirName: String? = null
    private set

fun setHomeDir(dirName: String) {
    if (homeDirName != null) {
        return
    }
    homeDirName = dirName
}

/*
* 私有存储 data/data/包名/...图片
* 根据文件名，返回绝对路径
* */
fun pathOfPrivateImg(fileName: String): String {
    val file = File(Box.ctx().filesDir, "image")
    if (!file.exists()) {
        file.mkdir()
    }
    return file.absolutePath + "/" + fileName
}

/*
* 私有存储 data/data/包名/...视频
* 根据文件名，返回绝对路径
* */
fun pathOfPrivateVideo(fileName: String): String {
    val file = File(Box.ctx().filesDir, "video")
    if (!file.exists()) {
        file.mkdir()
    }
    return file.absolutePath + "/" + fileName
}

/*
* 私有存储 data/data/包名/...声音
* 根据文件名，返回绝对路径
* */
fun pathOfPrivateSound(fileName: String): String {
    val file = File(Box.ctx().filesDir, "sound")
    if (!file.exists()) {
        file.mkdir()
    }
    return file.absolutePath + "/" + fileName
}

/*
* 私有存储 data/data/包名/...文档
* 根据文件名，返回绝对路径
* */
fun pathOfPrivateDoc(fileName: String): String {
    val file = File(Box.ctx().filesDir, "doc")
    if (!file.exists()) {
        file.mkdir()
    }
    return file.absolutePath + "/" + fileName
}

/*
* 专有存储 Android/data/包名/...文档
* 根据文件名，返回绝对路径
* */
fun pathOfSpecDoc(fileName: String): String {
    val file = Box.ctx().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)
    file ?: return pathOfPrivateDoc(fileName)
    return Box.ctx()
        .getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)!!.absolutePath + "/" + fileName
}

/*
* 专有存储 Android/data/包名/...图片
* 根据文件名，返回绝对路径
* */
fun pathOfSpecImg(fileName: String): String {
    val file = Box.ctx().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    file ?: return pathOfPrivateImg(fileName)
    return Box.ctx()
        .getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!.absolutePath + "/" + fileName
}

/*
* 专有存储 Android/data/包名/...视频
* 根据文件名，返回绝对路径
* */
fun pathOfSpecVideo(fileName: String): String {
    val file = Box.ctx().getExternalFilesDir(Environment.DIRECTORY_MOVIES)
    file ?: return pathOfPrivateVideo(fileName)
    return Box.ctx()
        .getExternalFilesDir(Environment.DIRECTORY_MOVIES)!!.absolutePath + "/" + fileName
}

/*
* 专有存储 Android/data/包名/...音频
* 根据文件名，返回绝对路径
* */
fun pathOfSpecSound(fileName: String): String {
    val file = Box.ctx().getExternalFilesDir(Environment.DIRECTORY_MUSIC)
    file ?: return pathOfPrivateSound(fileName)
    return Box.ctx()
        .getExternalFilesDir(Environment.DIRECTORY_MUSIC)!!.absolutePath + "/" + fileName
}

/*
* 专有存储 Android/data/包名/...下载
* 根据文件名，返回绝对路径
* */
fun pathOfSpecDownload(fileName: String): String {
    val file = Box.ctx().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
    file ?: return pathOfPrivateDoc(fileName)
    return Box.ctx()
        .getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!.absolutePath + "/" + fileName
}


/*
* 外部存储 sdcard/download/homedir/...下载
* 根据文件名，返回绝对路径
* */
fun pathOfExtDownload(fileName: String): String {
    if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
        return tryCatchWithReSult {
            val file =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            file ?: pathOfSpecDownload(fileName)
            val dir = homeDirName ?: "app"
            val fileDir =
                File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath + "/" + dir)
            if (!fileDir.exists()) {
                fileDir.mkdirs()
            }
            fileDir.absolutePath + "/" + fileName
        } ?: pathOfSpecDownload(fileName)
    } else {
        return pathOfSpecDownload(fileName)
    }

}


/*
* 外部存储 sdcard/download/homedir/...图片
* 根据文件名，返回绝对路径
* */
fun pathOfExtImg(fileName: String): String {
    if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
        return tryCatchWithReSult {
            val file =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            file ?: pathOfSpecImg(fileName)
            val dir = homeDirName ?: "app"
            val fileDir =
                File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).absolutePath + "/" + dir)
            if (!fileDir.exists()) {
                fileDir.mkdirs()
            }
            fileDir.absolutePath + "/" + fileName
        } ?: pathOfSpecImg(fileName)
    } else {
        return pathOfSpecImg(fileName)
    }
}

/*
* 外部存储 sdcard/download/homedir/...图片
* 根据文件名，返回绝对路径
* */
fun pathOfExtVideo(fileName: String): String {
    return pathOfExtImg(fileName)
}


/*
* 外部存储 sdcard/download/homedir/...下载
* 根据文件名，返回绝对路径
* */
fun pathOfExtSound(fileName: String): String {
    if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
        return tryCatchWithReSult {
            val file =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
            file ?: pathOfSpecSound(fileName)
            val dir = homeDirName ?: "app"
            val fileDir =
                File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).absolutePath + "/" + dir)
            if (!fileDir.exists()) {
                fileDir.mkdirs()
            }
            fileDir.absolutePath + "/" + fileName
        } ?: pathOfSpecSound(fileName)
    } else {
        return pathOfSpecSound(fileName)
    }
}
